/*
 * TimeStampUtils.cpp
 *
 *  Created on: Oct 11, 2014
 *      Authors: Avihay Avitan & Stefan Farcas
 */

#include "TimeStampUtils.h"



string getNowTimeStamp()
{
	return getTimeStampOf(getNowLongTimeSeconds(),getNowLongTimeNanos());
}

string getTimeStampOf(long time , long int nsec)
{
	struct tm * timeinfo;
    char buffer [30];

    timeinfo = localtime (&time);
	strftime (buffer,30,"[%d/%m/%Y,%H:%M:%S.",timeinfo);

	string line = "";
	line.append(buffer);
	line.append(getMiliSecOf( nsec));
    line.append("] ");

    return line;
}

string getTimeStampOf(long time )
{
	struct tm * timeinfo;
    char buffer [30];

    timeinfo = localtime (&time);
	strftime (buffer,30,"[%d/%m/%Y,%H:%M:%S] ",timeinfo);

    return buffer;
}

string getMiliSecOf( long int nsec)
{
	long int milis = nsec/1000000;

	return milis == 0 ? "000" :
			milis < 10 ? "00"+int2str(milis) :
			  milis<100 ? "0"+int2str(milis) :
				 int2str(milis);
}

long getNowLongTimeSeconds()
{
	timespec timespc;
	clock_gettime(CLOCK_REALTIME, &timespc);
    return timespc.tv_sec;
}

long int getNowLongTimeNanos()
{
	timespec timespc;
	clock_gettime(CLOCK_REALTIME, &timespc);
    return timespc.tv_nsec;
}


long getNowTimeWithSecOffset(int offset)
{
	return getNowLongTimeSeconds()+offset;
}

